budgetThird = 1;
const treeFun = (function() {
	let rateparams = {
		username: getUsername,
		budgetYear: localStorage.getItem("year"),
		budgetMonth: localStorage.getItem("month"),
		dept: '',
		status: 2,
		chartPage: 70404,
		chartNo: 5
	}
	let Request = new Object();
	Request = GetRequest();
	const index = Request['index'];
	const type = decodeURI(Request['type'])
	$.get(
		`${API_URL}getdata`, {
			...rateparams,
		},
		function(res) {
			let re = eval("(" + res + ")");
			let data = re.items[0].charts[0].data;
			$.get(
				`${API_URL}${localStorage.getItem("treeTotal")}`, {},
				function(res) {
					const re = eval("(" + res + ")");
					const data = re.items[0].charts[0].data[0];
					document.querySelector('.vessel .aside').innerHTML += `
										<div class="statistics">
											<h4>执行总额</h4>
											<h3>${data.key01}<span>万元</span></h3>
										</div>`
				}
			)
		}
	)
	renderAsideTree()

	function renderAsideTree() {
		$.get(`${API_URL}${localStorage.getItem('treeAsideTree')}`, {}, function(res) {
			const data = eval("(" + res + ")").items[0].charts[0].data;
			if (data.length > 1) {
				const trees = data.map((item, i) => {
					if (i === data.length - 1) {
						return `<h4>${item.key02}</h4>`
					} else {
						return `<span data-index="${i}">${item.key02}/</span>`
					}
				}).join('')
				console.log(trees)
				$('.vessel .aside').prepend('<div class="title"></div>')
				$('.vessel .aside .title').html(trees)
			}
			$('.vessel .aside').on('click','.title span',function(){
				const index = $(this)[0].dataset.index
				localStorage.setItem('url', data[index].key03)
				if(data[index].key04){
					localStorage.setItem('multiUrl', data[index].key04)
					localStorage.setItem('isMulti','true')
				}
				window.location.href = `budget-enforcement-total.html`;
			})
		})
		
	}
	// getratestate()

	// function getratestate() {
	// 	$.get(
	// 		`${API_URL}getdata`, {
	// 			...rateparams,
	// 			chartPage: 70402,
	// 			budgetNo: localStorage.getItem("budgetID"),
	// 			chartNo: 3
	// 		},
	// 		function(res) {
	// 			let re = eval("(" + res + ")");
	// 			if(re.items[0].charts[0].data.length>0){
	// 					document.querySelector('#rateMask .mask-scroll .state .p').innerHTML = re.items[0]
	// 						.charts[0].data[0].key01
	// 				}

	// 		})
	// }


	//月度到款执行
	function getMonPayComplete(dataurl) {
		$.get(
			`${API_URL}${dataurl}`, {},
			function(res) {
				const re = eval("(" + res + ")");
				const data = re.items[0].charts[0].data;
				console.log(data)
				if (data.length === 0) {
					if (document.querySelector('#monthlj')) {
						document.querySelector('#monthlj') && document.querySelector('#monthlj').remove()
						document.querySelector('.month').innerHTML += `<div class="nowrap">
										<div class="noData">
											<img src="assets/images/mask/budget-All-IncomingPayment.svg" >
											<p>暂无数据</p>
										</div>
									</div>`
					}
					return
				}
				if (document.querySelector('.nowrap')) {
					document.querySelector('.nowrap').remove();
					document.querySelector('.month').innerHTML += `<div id="monthlj"></div>`
				}
				let xData = [];
				let currYearBudget = [];
				const legend = re.items[0].charts[0].chart_name.split(' ').splice(2);
				const legendData = legend.map(item => {
					let newIt = item.split('-').splice(1)[0]
					if (newIt.indexOf('{') > -1) {
						newIt = newIt.replace('{', '')
					}
					if (newIt.indexOf('}') > -1) {
						newIt = newIt.replace('}', '')
					}
					return newIt
				})

				for (var i = 0; i < data.length; i++) {
					xData.push(+data[i].key01);
					currYearBudget.push(data[i].key02);
				}

				let Data = [{
					name: "执行金额",
					type: "bar",
					barWidth: '30%',
					barGap: 0,
					itemStyle: {
						color: "#50CD89",
					},
					data: currYearBudget,
				}, ];
				have("monthlj") ? mulitYBBar("monthlj", xData, legendData, Data, '月', true, false) : "";
			})

	}


	// document.querySelector('#rateMask .mask-scroll .state div .edit').onclick = function() {
	// 	document.querySelector(
	// 			'#editMask .mask-content textarea').value = document
	// 		.querySelector('#rateMask .mask-scroll .state .p').innerText
	// 	toggle()
	// }


	// document.querySelector('#editMask .mask-content .btn button.cancle').onclick = function() {
	// 	toggle()
	// }

	// function toggle() {
	// 	document.querySelector('#editMask').style.display = document.querySelector(
	// 		'#editMask').style.display !== 'block' ? 'block' : 'none'
	// }


	// document.querySelector('#editMask .mask-content .btn button.save').onclick = () => {
	// 	$.get(
	// 		`${API_URL}getdata`, {
	// 			...rateparams,
	// 			chartNo: 4,
	// 			chartPage: 70402,
	// 			budgetNo: localStorage.getItem("budgetID"),
	// 			key01: document.querySelector(
	// 				'#editMask .mask-content textarea').value
	// 		},
	// 		function(res) {
	// 			let re = eval("(" + res + ")");
	// 			alert(re.items[0].charts[0].chart_name)
	// 			getratestate()
	// 			toggle()
	// 		})
	// }


	getTree()

	function GetRequest() {
		var url = encodeURI(window.location.search); //获取url中"?"符后的字串
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	}

	function getTree() {
		$.get(
			`${API_URL}${localStorage.getItem("treeCourse")}`, {},
			function(res) {
				let re = eval("(" + res + ")");
				const data = re.items[0].charts[0].data;
				console.log(data)
				const _data = data.map(item => item['key02']).filter((item, index) => {
					return data.map(item => item['key02']).indexOf(item) === index
				})
				const allData = [{
					name: type,
					value: 0,
					children: []
				}]
				for (let i = 0; i < _data.length; i++) {
					allData[0].children.push({
						name: _data[i],
						children: []
					})
					for (let j = 0; j < data.length; j++) {
						if (data[j]['key02'] === _data[i]) {
							allData[0].children[i].children.push({
								name: data[j].key01,
								children: [{
									name: '执行金额',
									num: data[j].key03 ? data[j].key03 : '0.00',
									pname: data[j].key01,
									course: data[j].key04
								}]
							})
						}
					}
				}
				console.log(allData)
				// have("tree-chart") ? dimensionTree1("tree-chart", allData,colors) : "";
				have("tree-chart") ? dimensionRelation("tree-chart", allData) : "";
			})


	}

	document.querySelector('.mask-wrap .mask-title img').onclick = function() {
		document.querySelector('#rateMask').style.display = 'none'
	}
	return {
		getMonPayComplete,
		// getratestate
	}
})()

function treeNodeclick(param) {
	if (param.data.num) {
		treeFun.getMonPayComplete(param.data.course)
		document.querySelector('#rateMask').style.display = 'block'
	}
}
